﻿using MyWPF.StaticData;
using NPOI.SS.Formula.Functions;
using Prism.Mvvm;
using ScottPlot;
using ScottPlot.Plottable;
using ScottPlot.Renderable;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity.Infrastructure;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace MyWPF.ViewModel.Subpage.MainPage_Son
{
    public class VM_MainPage_Son2 : BaseViewModel
    {
        public VM_MainPage_Son2() 
        {
            RefreshScottPlot();
        }

        #region 属性绑定/事件：刷新ScottPlot界面数据
        private void RefreshScottPlot()
        {
            ChartDatas = new ObservableCollection<SpModel>();
            Random random = new Random();
            double[] datas1 = DataGen.RandomWalk(random, (int)1e6);
            double[] datas2 = DataGen.RandomWalk(random, (int)1e6);
            double[] datas3 = DataGen.RandomWalk(random, (int)1e6);
            Task.Run(() =>
            {
                for (int i = 0; i < datas1.Length; i++)
                {
                    ChartDatas.Add(new SpModel
                    {

                        DateTime = DateTime.Now.AddSeconds(i),
                        Values1 = datas1[i],
                        Values2 = datas2[i],
                        Values3 = datas3[i]
                    });
                }
            });

            
        }
        public ObservableCollection<SpModel> ChartDatas
        {
            get { return _chartDatas; }
            set { SetProperty(ref _chartDatas, value); }
        }
        private ObservableCollection<SpModel> _chartDatas;
        #endregion

    }
    public class ScottPlotExtension
    {
        //附加属性
        //数据来源https://www.bilibili.com/video/BV1V14y1W7uK?p=18&spm_id_from=pageDriver&vd_source=9b66fc8ecc3cd101e0f0023b47c01218
        [Obsolete]
        public static ObservableCollection<SpModel> GetValues(DependencyObject obj)
        {
            return (ObservableCollection<SpModel>)obj.GetValue(ValuesProperty);
        }

        [Obsolete]
        public static void SetValues(DependencyObject obj, ObservableCollection<double> value)
        {
            obj.SetValue(ValuesProperty, value);
        }

        [Obsolete]
        public static readonly DependencyProperty ValuesProperty =
            DependencyProperty.RegisterAttached(
                "Values",
                typeof(ObservableCollection<SpModel>),
                typeof(ScottPlotExtension),
                new PropertyMetadata(null, new PropertyChangedCallback(OnValuesChanged)));

        //需要将数据处理，将Values属性获取到的数据提供给控件
        [Obsolete]
        private static void OnValuesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var datas = e.NewValue as ObservableCollection<SpModel>;
            var obj = d as WpfPlot;

            List<string> xList = new List<string>();
            List<double> dataList1 = new List<double>();
            List<double> dataList2 = new List<double>();
            List<double> dataList3 = new List<double>();

            //提取数据
            datas.ToList().ForEach(item =>
            {
                xList.Add(item.DateTime.ToString("mm:ss"));
                dataList1.Add(item.Values1);
                dataList2.Add(item.Values2);
                dataList3.Add(item.Values3);
            });
            TimeSpan ts = TimeSpan.FromSeconds(1); // time between data points
            double sampleRate = (double)TimeSpan.TicksPerDay / ts.Ticks;
            //obj.plt.AddSignal(dataList.ToArray());
            var signalPlot1 = obj.plt.AddSignal(dataList1.ToArray(), sampleRate, label : "参数A");
            var signalPlot2 = obj.plt.AddSignal(dataList2.ToArray(), sampleRate, label: "参数B");
            var signalPlot3 = obj.plt.AddSignal(dataList3.ToArray(), sampleRate, label: "参数C");
            DateTime dateTime = DateTime.Now;
            signalPlot1.OffsetX = dateTime.ToOADate();
            signalPlot2.OffsetX = dateTime.ToOADate();
            signalPlot3.OffsetX = dateTime.ToOADate();
            //未知
            obj.plt.Legend();
            //多Y轴
            obj.plt.YAxis.Label("参数A", Color.FromArgb(0xff, 0x6e, 0x6e, 0x6e));
            var yAxis2 = obj.plt.AddAxis(Edge.Left);
            yAxis2.Label("参数B", Color.FromArgb(0xff, 0x6e, 0x6e, 0x6e));
            var yAxis3 = obj.plt.AddAxis(Edge.Left);
            yAxis3.Label("参数C", Color.FromArgb(0xff, 0x6e, 0x6e, 0x6e));
            signalPlot2.YAxisIndex = yAxis2.AxisIndex;
            signalPlot3.YAxisIndex = yAxis3.AxisIndex;
            //设置横坐标为时间格式
            obj.plt.XAxis.DateTimeFormat(true);
            
            obj.plt.Title("3*100万个点");
            
            //设置样式
            obj.plt.Style(
                //背景色：figureBackground
                figureBackground: Color.FromArgb(0x00, 0x6e, 0x6e, 0x6e),
                //坐标系背景色：dataBackground
                dataBackground: Color.FromArgb(0x00, 0x6e, 0x6e, 0x6e),
                //坐标系颜色：tick
                tick: Color.FromArgb(0xff, 0x6e, 0x6e, 0x6e),
                //标题颜色
                titleLabel: Color.FromArgb(0xff, 0x6e, 0x6e, 0x6e)
                ) ;
            ////给指定点(27,-0.25)添加箭头，箭头起始点(23,-5)
            //var arrow1 = obj.plt.AddArrow(27, -.25, 23, -.5);
            //arrow1.Color = System.Drawing.Color.Red;
            //arrow1.MinimumLengthPixels = 10;
            //var arrow2 = obj.plt.AddArrow(429543, 48.6325, 429543, 60);
            //arrow2.Color = System.Drawing.Color.Orange;
            //arrow2.MinimumLengthPixels = 10;
            ////给指定点添加一个标签
            //obj.plt.AddTooltip(label: "500000,40", x: 500000, y: 40);
            obj.Refresh();
        }
    }
    public class SpModel
    {
        public DateTime DateTime { get; set; }
        public double Values1 { get; set; }
        public double Values2 { get; set; }
        public double Values3 { get; set; }
    }
}
